﻿open System
open System.IO
open Microsoft.FSharp.Text.Lexing

open TinyJava.Ast
open Lexer
open Parser
open Environment
open TypeRules
open ExceptionMonad

let Compile() =
  try
    let inputChannel = new StreamReader("Program.tj")
    let lexbuf = LexBuffer<char>.FromTextReader inputChannel
    let ast = Parser.start Lexer.tokenize lexbuf //parse the file and create the AST
    let partial_table = fill_table ast //initialize the symbol table with class,field and method infos
    let empty_block_table = {Vars = [];Parent = None;Owner = None;Child = None}
    let environment = {SymbolTable = partial_table;BlockTable = empty_block_table;CurrentClass = "";NeededReturns = 0}
    let type_check_result = execute eval_program ast environment
    match type_check_result with
    | Statement(_,typed_env) -> ()
    | Exception(error) -> failwith error
    1
  with
    | Failure(msg) -> 
        printfn "%s" msg
        printfn "%s" "Build aborted..."
        0
    | :? System.Exception -> 
        printfn "Parse error"
        printfn "%s" "Build aborted..."
        0

Compile() |> ignore